
#ifndef __SQLITE_DB_H
#define __SQLITE_DB_H

#include <vector>
#include <sqlite3.h>
#include "../iface/debug.h"

// Maybe write in hourly backups somewhere?

class SQLiteDB
{
   public:
      SQLiteDB(const char *filename, int persists);
      ~SQLiteDB();
      static int TestResultsCallback(void *unused, int argc, char **argv, char **azColName);
      int SQLExecTest(const char *query);
      int SQLInstantExec(const char *query);
      int SQLInstantExec_unsafe(const char *query);
      int SQLInstantExecf(const char *query, ...);
      int SQLInstantExecf_unsafe(const char *query, ...);
      std::vector<char *> *SQLScanRowText(const char *csql);
      std::vector<char *> *SQLScanRowText_unsafe(const char *csql);
      std::vector<char *> *SQLScanRowTextf(const char *csql, ...);
      std::vector<char *> *SQLScanRowTextf_unsafe(const char *csql, ...);
      std::vector<char *> *SQLScanAllText(const char *csql);
      std::vector<char *> *SQLScanAllTextf(const char *csql, ...);
      void Backup();
      int DumpDatabaseToSQL(const char *filename);
      int DumpTableToSQL(FILE *FP, const char *tablename);
      int isWhitespace(char wht);
      int LoadSQL(const char *filename);
      static void StringSanitiser(char *sanstr);
      sqlite3_stmt *SQLCompile(const char *query);
      int SQLScanRow_unsafe(sqlite3_stmt *sql, const char *rowformat, ...);
      std::vector<char *> *SQLScanRowText_unsafe(sqlite3_stmt *sql);
      std::vector<char *> *SQLScanAllText_unsafe(sqlite3_stmt *sql);
      int SQLFinalize(sqlite3_stmt *sql);
      void EnsureTablesAreCreated(int notables, ...);
      int SQLGetSingleInt_unsafe(int *result, sqlite3_stmt *sql);
      int SQLGetSingleDouble_unsafe(double *result, sqlite3_stmt *sql);
      int SQLGetSingleString_unsafe(char **result, sqlite3_stmt *sql);
      int SQLGetSingleIntf_unsafe(int *result, const char *csql, ...);
      int SQLGetSingleDoublef_unsafe(double *result, const char *csql, ...);
      int SQLGetSingleStringf_unsafe(char **result, const char *csql, ...);
      int SQLGetSingleIntf(int *result, const char *csql, ...);
      int SQLGetSingleDoublef(double *result, const char *csql, ...);
      int SQLGetSingleStringf(char **result, const char *csql, ...);
   protected:
      sqlite3 *db;
      sqlite3_mutex *dbmutex;
      char *dumpfilename;
};

#endif
